tg-me.com/C_Codings/205
Last Update:
143. Program to allocate memory using malloc() and free memory using free().
#include<stdio.h>
#include<stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter total number of elements : ");
scanf("%d", &n);
ptr = (int *) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Error! Memory not allocated.");
return 0;
}
printf("Enter elements of array : \n");
for (i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Elements are :\n");
for (i = 0; i < n; i++) {
printf("%d\n", ptr[i]);
}
free(ptr);
return 0;
}
@C_Codings
BY C Language π¨βπ»
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Share with your friend now:
tg-me.com/C_Codings/205